package com.ede.roverhu.simplebackstage.Utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

/**
 * Created by Rover on 2016/10/7 .
 * Email 357319517@qq.com
 */
public class IntentUtils {
    /**
     * 开启新的Activity
     *
     * @param context
     * @param cls
     */
    public static void startActivity(Activity context, Class cls) {

        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    /**
     * 开启新的Activity并结束自身
     *
     * @param context
     * @param cls
     */
    public static void startActivityAndFinish(Activity context, Class cls) {

        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        context.finish();
    }

    /**
     * 延迟开启一个Activity
     *
     * @param context
     * @param cls
     */
    public static void startActivityForDelay(final Activity context, final Class cls, final long delaytime) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(delaytime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(context, cls);
                context.startActivity(intent);
            }
        }.start();
    }

    /**
     * 、
     * 延迟开启一个新的Activity并且同时结束自身
     *
     * @param context
     * @param cls
     * @param delaytime
     */
    public static void startActivityForDelayAndFinish(final Activity context, final Class cls, final long delaytime) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(delaytime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(context, cls);
                context.startActivity(intent);
                context.finish();
            }
        }.start();
    }

    public static void install(Activity activity, File file) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        activity.startActivity(intent);
    }
}
